﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChoDiaOc_HK_WPF.Models;

namespace ChoDiaOc_HK_WPF.Controls {
    public partial class GiaHanDangKyController {

        private static GiaHanDangKyController controller = null;

        public static GiaHanDangKyController GetInstance() {
            if (controller == null) {
                controller = new GiaHanDangKyController();
            }
            return controller;
        }

        private GiaHanDangKyController() {
        }

        private PHIEUDANGKY phieuDangKy;

        public PHIEUDANGKY PhieuDangKy {
            get { return phieuDangKy; }
            set { phieuDangKy = value; }
        }

        private KHACHHANG khachHang;

        public KHACHHANG KhachHang {
            get { return khachHang; }
            set { khachHang = value; }
        }

        private DIAOC diaOc;

        public ChoDiaOc_HK_WPF.DIAOC DiaOc {
            get { return diaOc; }
            set { diaOc = value; }
        }
        private LOAIDIAOC loaiDiaOc;

        public ChoDiaOc_HK_WPF.LOAIDIAOC LoaiDiaOc {
            get { return loaiDiaOc; }
            set { loaiDiaOc = value; }
        }

        private String soTienNop;
        public System.String SoTienNop {
            get { return soTienNop; }
            set { soTienNop = value; }
        }
        public void TinhToanGiaHan() {
            phieuDangKy.DenNgay = phieuDangKy.DenNgay.AddDays(30);
            int soNgay = (phieuDangKy.DenNgay - phieuDangKy.TuNgay).Days;
            int soLanGiaHan = soNgay / 30 - 1;
            if (soLanGiaHan == 1) {
                soTienNop = (double.Parse(phieuDangKy.SoTien.ToString()) * DatabaseWrapper.GetInstance().GetThamSoTiLeGiaGiaHan()).ToString();
                phieuDangKy.SoTien += phieuDangKy.SoTien * Decimal.Parse(DatabaseWrapper.GetInstance().GetThamSoTiLeGiaGiaHan().ToString());                
            } else {
                decimal unit = decimal.Parse(                    (
                        double.Parse(
                            phieuDangKy.SoTien.ToString()) / (1 + DatabaseWrapper.GetInstance().GetThamSoTiLeGiaGiaHan() + (soLanGiaHan - 2) * DatabaseWrapper.GetInstance().GetThamSoTiLeGiaGiaHan())
                    ).ToString()
                );

                soTienNop = (double.Parse(unit.ToString()) * DatabaseWrapper.GetInstance().GetThamSoTiLeGiaGiaHan()).ToString();
            }
            phieuDangKy.DenNgay = phieuDangKy.DenNgay.Subtract(new TimeSpan(30, 0, 0));

        }

        public bool GiaHan() {
            phieuDangKy.SoTien += decimal.Parse(SoTienNop) * Decimal.Parse(DatabaseWrapper.GetInstance().GetThamSoTiLeGiaGiaHan().ToString());
            phieuDangKy.DenNgay = phieuDangKy.DenNgay.AddDays(30);

            return DatabaseWrapper.GetInstance().UpdatePhieuDangKy(phieuDangKy.MaPhieuDangKy, phieuDangKy.DenNgay, phieuDangKy.SoTien);
        }
    }
}
